package com.ybg.bookmember.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.ybg.bookcommons.dto.BookinfosDTO;
import com.ybg.bookcommons.dto.ReservationsDTO;
import com.ybg.bookcommons.dto.ReturnDTO;
import com.ybg.bookcommons.vo.BookinfosVO;
import com.ybg.bookmember.services.BookinfosService;

import com.ybg.bookmember.services.ReservationsService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberCtrl {
    @Resource
    private BookinfosService bookinfosService;
    @Resource
    private StringRedisTemplate reidsTemplate;
    @Resource
    private ReservationsService reservationsService;
    /**
     * 用户多条件查询图书 按照书名 按照分类 按照作者 按照出版社 进行模糊查询
     * @param book
     * @param current
     * @param size
     * @return
     */
    @RequestMapping(value = "/searchbook",method = RequestMethod.GET)
    public Page<BookinfosDTO> search(BookinfosVO book,
                                     int current,int size){
        return bookinfosService.selectBooksMutilCondition(book,current,size);
    }

    /**
     * 图书预约
     * @param isbn
     * @return
     */
    @RequestMapping(value = "/rese/{isbn}",method = RequestMethod.GET)
    public List<ReservationsDTO> rese(@PathVariable String isbn, HttpServletRequest request){
        //根据token获取用户id
        String token = request.getHeader("Authorization");
        String user = reidsTemplate.opsForValue().get(token);
        Long userid = Long.parseLong(user.split(",")[0]);
        List<ReservationsDTO> dto = reservationsService.reserv(isbn, userid);
        return dto;
    }

    /**
     * 取消预约接口
     * @param isbn 图书isbn
     * @return
     */
    @RequestMapping(value="/destory/{isbn}",method = RequestMethod.GET)
    public ReturnDTO destoryReservation(@PathVariable String isbn,HttpServletRequest request){
        //根据token获取用户id
        String token = request.getHeader("Authorization");
        String user = reidsTemplate.opsForValue().get(token);
        Long userid = Long.parseLong(user.split(",")[0]);
        boolean res = reservationsService.destoryReserv(isbn, userid);
        if(res) {
            return ReturnDTO.builder().status("success").message("用户:" + userid + "已经取消:" + isbn + "预约").build();
        }else{
            return ReturnDTO.builder().status("fail").message("用户:" + userid + "无法取消预约").build();
        }
    }


}
